//
//  ClassReadingsTableViewController.m
//  Digital Seamonsters
//
//  Created by Adam Cue on 2/22/11.
//  Copyright Adam Cue 2011.
//
//  This program is licensed and distributed under the terms of the GNU General Public License.  
//	The text for the license can be found in the license.txt file in this project.
//

#import "ClassReadingsTableViewController.h"
#import "JSON.h"

@implementation ClassReadingsTableViewController
@synthesize listOfClasses = _listOfClasses;
@synthesize delegate;
@synthesize forLessons;

- (void)dealloc {
   self.listOfClasses = nil;
   self.delegate = nil;
   
   [super dealloc];
}

#pragma mark ExternalMethods
- (void)loadClassList{
	NSString *jsonString = [[NSString alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:LIST_CLASSES_URL]] encoding:NSUTF8StringEncoding];
	self.listOfClasses = [jsonString JSONValue];
   dispatch_async(dispatch_get_main_queue(), ^{[self.tableView reloadData];});
}

#pragma mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   if(forLessons == YES){
      return [self.listOfClasses count];
	}
	return 1 + [self.listOfClasses count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   static NSString *ListOfClassesCellIdentifier = @"ListOfClassesCell";
   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ListOfClassesCellIdentifier];
   if (cell == nil) {
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ListOfClassesCellIdentifier] autorelease];
   }
	if(indexPath.row == 0 && forLessons == NO){
		cell.backgroundColor = [UIColor clearColor];
		cell.textLabel.textColor = [UIColor	whiteColor];
		cell.textLabel.text = @"Saved Readings";
		return cell;
	}
	cell.backgroundColor = [UIColor clearColor];
	NSDictionary *info;
    if(forLessons == NO){
		info = [self.listOfClasses objectAtIndex:(indexPath.row-1)];
	}
	else{
		info = [self.listOfClasses objectAtIndex:indexPath.row];
	}
	cell.textLabel.text = [info objectForKey:@"name"];
	cell.textLabel.textColor = [UIColor whiteColor];
	NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
	[dateFormatter setDateFormat:@"yyyy-MM-dd"];
	NSDate *date = [dateFormatter dateFromString:[info objectForKey:@"date"]];
	[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
	cell.detailTextLabel.text = [dateFormatter stringFromDate:date];
	[dateFormatter release];
   return cell;
}

#pragma mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	if(indexPath.row == 0 && forLessons == NO){
		[delegate classClicked:-1];
		return;
	}
	NSDictionary *info;
	if(forLessons == NO){
		info = [self.listOfClasses objectAtIndex:(indexPath.row-1)];
	}
	else{
		info = [self.listOfClasses objectAtIndex:indexPath.row];
	}
	[delegate classClicked:[[info objectForKey:@"id"] intValue]];
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
	cell.backgroundColor = [UIColor clearColor];
}

@end

